package ai.mspbots.test.service.impl;

import ai.mspbots.test.dto.AzureAIConfig;
import ai.mspbots.test.entity.TenantIntegration;
import ai.mspbots.test.mapper.TenantIntegrationMapper;
import ai.mspbots.test.service.TenantIntegrationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class TenantIntegrationServiceImpl extends ServiceImpl<TenantIntegrationMapper, TenantIntegration>
    implements TenantIntegrationService {

    @Override
    public List<TenantIntegration> getByTenantCode(Long tenantCode) {
        LambdaQueryWrapper<TenantIntegration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TenantIntegration::getTenantCode, tenantCode);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<TenantIntegration> getByIntegrationId(Long integrationId) {
        LambdaQueryWrapper<TenantIntegration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TenantIntegration::getIntegrationId, integrationId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<AzureAIConfig> getByIntegrations(Long integrationId) {
        return baseMapper.getByIntegrations(integrationId);
    }

    @Override
    public TenantIntegration getByTenantAndIntegration(Long tenantCode, Long integrationId) {
        LambdaQueryWrapper<TenantIntegration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TenantIntegration::getTenantCode, tenantCode)
               .eq(TenantIntegration::getIntegrationId, integrationId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean existsByTenantAndIntegration(Long tenantCode, Long integrationId) {
        LambdaQueryWrapper<TenantIntegration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TenantIntegration::getTenantCode, tenantCode)
               .eq(TenantIntegration::getIntegrationId, integrationId);
        return baseMapper.selectCount(wrapper) > 0;
    }

    @Override
    public boolean enableIntegration(Long id) {
        LambdaUpdateWrapper<TenantIntegration> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TenantIntegration::getId, id)
               .set(TenantIntegration::getStatus, 1)
               .set(TenantIntegration::getUpdateDate, LocalDateTime.now());
        return update(wrapper);
    }

    @Override
    public boolean disableIntegration(Long id) {
        LambdaUpdateWrapper<TenantIntegration> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TenantIntegration::getId, id)
               .set(TenantIntegration::getStatus, 0)
               .set(TenantIntegration::getUpdateDate, LocalDateTime.now());
        return update(wrapper);
    }

    @Override
    public boolean updateParamsValues(Long id, String paramsValues) {
        LambdaUpdateWrapper<TenantIntegration> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TenantIntegration::getId, id)
               .set(TenantIntegration::getParamsValues, paramsValues)
               .set(TenantIntegration::getUpdateDate, LocalDateTime.now());
        return update(wrapper);
    }

    @Override
    public boolean batchUpdateStatus(List<Long> ids, Integer status) {
        LambdaUpdateWrapper<TenantIntegration> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(TenantIntegration::getId, ids)
               .set(TenantIntegration::getStatus, status)
               .set(TenantIntegration::getUpdateDate, LocalDateTime.now());
        return update(wrapper);
    }

    @Override
    public List<TenantIntegration> getByStatus(Integer status) {
        LambdaQueryWrapper<TenantIntegration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TenantIntegration::getStatus, status);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<TenantIntegration> getByJsonValue(String jsonValue) {
        return baseMapper.selectByJsonValue(jsonValue);
    }
}